/**
    @author: dt
    @date: 2025-06-23
    @description: 行为树管理器
*/

using UnityEngine;

namespace DLib.BehaviorTree
{
    /// <summary>
    /// 行为树管理器
    /// 负责管理和执行行为树
    /// 在Update中持续评估根节点
    /// </summary>
    public class BehaviorTree
    {
        /// <summary>行为树的根节点</summary>
        public Node root = null;
        
        /// <summary>行为树是否正在运行</summary>
        public bool isRunning = false;

        /// <summary>行为树的黑板</summary>
        public Blackboard blackboard = null;

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public BehaviorTree()
        {
            blackboard = new Blackboard();
        }

        /// <summary>
        /// 带黑板的构造函数
        /// </summary>
        /// <param name="blackboard">黑板实例</param>
        public BehaviorTree(Blackboard blackboard)
        {
            this.blackboard = blackboard ?? new Blackboard();
        }

        /// <summary>
        /// Unity Start方法
        /// 如果根节点存在，开始运行行为树
        /// </summary>
        void Start()
        {
            if (root != null)
            {
                isRunning = true;
            }
        }

        /// <summary>
        /// Unity Update方法
        /// 每帧评估行为树根节点
        /// 当根节点不再运行中时，停止行为树并调用完成回调
        /// </summary>
        void Update()
        {
            if (isRunning && root != null)
            {
                NodeState state = root.Evaluate();
                
                if (state != NodeState.Running)
                {
                    isRunning = false;
                    OnBehaviorTreeComplete(state);
                }
            }
        }

        /// <summary>
        /// 设置行为树根节点
        /// </summary>
        /// <param name="rootNode">要设置为根节点的节点</param>
        public void SetRoot(Node rootNode)
        {
            root = rootNode;
            isRunning = true;
        }

        /// <summary>
        /// 重置行为树
        /// 重置所有复合节点的状态，并重新开始运行
        /// </summary>
        public void ResetTree()
        {
            if (root != null)
            {
                root.Reset();
            }
            if (blackboard != null)
            {
                blackboard.Clear();
            }
            isRunning = true;
        }

        /// <summary>
        /// 停止行为树
        /// </summary>
        public void StopTree()
        {
            isRunning = false;
        }

        /// <summary>
        /// 获取黑板
        /// </summary>
        /// <returns>黑板实例</returns>
        public Blackboard GetBlackboard()
        {
            return blackboard;
        }

        /// <summary>
        /// 设置黑板
        /// </summary>
        /// <param name="blackboard">黑板实例</param>
        public void SetBlackboard(Blackboard blackboard)
        {
            this.blackboard = blackboard ?? new Blackboard();
        }

        /// <summary>
        /// 行为树完成时的回调方法
        /// 子类可以重写此方法来处理完成事件
        /// </summary>
        /// <param name="finalState">行为树的最终状态</param>
        protected virtual void OnBehaviorTreeComplete(NodeState finalState)
        {
            Debug.Log($"Behavior Tree completed with state: {finalState}");
        }

        /// <summary>
        /// 获取当前行为树状态
        /// </summary>
        /// <returns>根节点的当前状态，如果没有根节点则返回失败</returns>
        public NodeState GetCurrentState()
        {
            if (root != null)
            {
                return root.GetState();
            }
            return NodeState.Failure;
        }
    }
} 